package com.blockmeta.bbs.immodule.corebean;

import com.blockmeta.bbs.immodule.dao.ChatMessage;

import java.util.ArrayList;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

public class MsgSendQueue {
    private Queue<ChatMessage> m_queuePacket;
    private final static int MaxPacketNum = 5;
    private IMMetaBase mIMMetaBase;

    public MsgSendQueue(IMMetaBase imMetaBase)
    {
        mIMMetaBase = imMetaBase;
        m_queuePacket = new ConcurrentLinkedQueue<ChatMessage>();
    }

    public void push(ChatMessage pkt)
    {
        if(m_queuePacket.size() > MaxPacketNum)
        {
            m_queuePacket.poll();
        }
        m_queuePacket.add(pkt);
    }

    public ChatMessage pop()
    {
        return m_queuePacket.poll();
    }

    public void sendAll()
    {
        ChatMessage pkt = m_queuePacket.poll();

        while(pkt != null)
        {
            mIMMetaBase.sendChatMessage(pkt);

            pkt = m_queuePacket.poll();
        }
    }

    public void clear()
    {
        m_queuePacket.clear();
    }
}
